/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.appintro.component;

import com.github.appintro.ResourceTable;
import com.github.appintro.utile.ResUtil;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * 自定义view
 */
public abstract class CustomComponent extends Image implements Component.DrawTask, Component.TouchEventListener {
    private final String IS_CIRCLE_BG = "isCircleBackground";
    /**
     * 画笔
     */
    protected Paint paint;
    /**
     * 圆形画笔
     */
    protected Paint circlePaint;

    /**
     * 画笔path
     */
    protected Path path;
    /**
     * 宽
     */
    protected int width;
    /**
     * 高
     */
    protected int height;

    private boolean isClick;
    /**
     * 长按显示圆形画笔默认颜色
     */
    private int circlceDefualtColor = 0x22000000;
    /**
     * 透明度值
     */
    protected int alphaColor = 0x22000000;
    /**
     * 长按显示圆形画笔颜色
     */
    protected int circlceColor = circlceDefualtColor;

    private boolean isCircleBg = false;

    public CustomComponent(Context context) {
        super(context);
    }

    public CustomComponent(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public CustomComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        boolean hasParam = attrSet.getAttr(IS_CIRCLE_BG).isPresent();
        if (hasParam) {
            isCircleBg = attrSet.getAttr(IS_CIRCLE_BG).get().getBoolValue();
        }
        circlePaint = new Paint();
        path = new Path();
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeWidth(10);
        paint.setColor(new Color(ResUtil.getColor(getContext(), ResourceTable.Color_appintro_icon_tint)));
        paint.setStyle(Paint.Style.STROKE_STYLE);
        width = getWidth();
        height = getHeight();
        addDrawTask(this);
        setTouchEventListener(this);
    }

    /**
     * 设置画笔路径
     */
    public abstract void setPath();

    /**
     * 设置提示语
     */
    public abstract void setDescription();

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (isCircleBg) {
            if (isClick) {
                circlePaint.setColor(new Color(0x22FFFFFF));
                canvas.drawCircle(width / 2, height / 2, width / 2, circlePaint);
            }
            circlePaint.setColor(new Color(circlceColor));
            canvas.drawCircle(width / 2, height / 2, width / 3, circlePaint);
        } else {
            if (isClick) {
                circlePaint.setColor(new Color(circlceColor));
                canvas.drawRect(new RectFloat(width / 8, width / 8, width * 7 / 8, width * 7 / 8), circlePaint);
            }
        }

        setPath();
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                isClick = true;
                invalidate();
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                isClick = false;
                invalidate();
                break;
        }
        return true;
    }
}
